/* BaseExportAction.java
 * Author: liuex
 * 2007-12-19 ����11:33:03
 *
 * Note: 
 */
package lex.proxyverfier.importexport;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.lex.net.proxy.ProxyServer;
import org.lex.utils.SystemProperties;


public abstract class AbstractExportProxyServer {
	public abstract StringBuilder format(StringBuilder dest, ProxyServer ps);

	public void export(ProxyServer[] servers, File dest) throws IOException {
		if (false == dest.exists() || false == dest.isFile()) {
			boolean b = dest.createNewFile();
			if (false == b)
				throw new IOException("Can't create specified file: " + dest);
		}
		StringBuilder buf = new StringBuilder();
		String newLine = SystemProperties.lineSeparator();
		BufferedWriter out = new BufferedWriter(new FileWriter(dest));
		for (int i = 0; i < servers.length; i++) {
			buf.setLength(0);
			this.format(buf, servers[i]);
			out.write(buf.toString());
			out.write(newLine);
		}
		out.close();
	}
}
